// pages/search/index.js
var app = getApp();
const getGoodsTypeUrl = '/Goods/getGoodsCategory';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading:1,//是否显示加载中图标
    filesSourceUri:'',//存储静态资源文件
    typeList:[]//商品类型列表
  },
  // 跳转首页对应的专题部分
  goHome: function (e) {
    wx.reLaunch({
      url: '/pages/home/home?id=' + e.currentTarget.dataset.keywords + '&cate_id=' + e.currentTarget.dataset.cate_id
    })
  },
  // 
  
  goBuyCar: function () {
    wx.reLaunch({
      url: '/pages/buyCar/buyCar',
    })
  },
  // 跳转搜索
  goSearch: function () {
    wx.reLaunch({
      url: '/pages/search/index',
    })
  },
  // 跳转我的
  goMy: function () {
    wx.reLaunch({
      url: '/pages/my/my',
    })
  },
  // 获取搜索索引
getGoodsType:function(){
  var _this = this;
  var requestData = {
    token: _this.data.token,
    version: app.setConfig.version,
    type: app.setConfig.type,
  }
  app.post(getGoodsTypeUrl, requestData, function (res) {
    if (res.code == 2000) {
      var typeList = res.data;
      var filesSourceUri = _this.data.filesSourceUri;
      for (var i = 0; i < typeList.length;i++){
        console.log(i);
        typeList[i].image = filesSourceUri + '/' + typeList[i].image
      }
      _this.setData({
        isLoading:0,
        typeList: typeList
      })
    }else{
      wx.showToast({
        title: res.msg,
        icon: "none",
        duration: 2000
      });
    }
  });
},

// 跳转搜索结果页
  goResult:function(e){
    console.log(e);
    wx.navigateTo({
      url: './result' + e.currentTarget.dataset.keywords
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var configs = wx.getStorageSync('configs');
    _this.setData({
      filesSourceUri: configs.static_url_http
    })
    _this.loop();
  },
  // 判断是否获取到token的值了
  loop: function () {
    var _this = this;
    var theToken = wx.getStorageSync("token");
    if (!theToken) {
      setTimeout(function () { _this.loop() }, 100);
    } else {
      _this.setData({
        token: theToken
      });
      _this.getGoodsType();
    }
  }
})